﻿// Developer Express Code Central Example:
// World Wide CRM Demo
// 
// The World Wide CRM demo is a next generation application with outstanding
// navigation capabilities and shows a reimagined view of customer contact and
// management. This demo showcases many of the most popular features of the
// WinForms Subscription.
// 
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3993

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.CrmDemo.Win.Data;
using DevExpress.XtraCharts;
using System.Diagnostics;

namespace DevExpress.CrmDemo.Win.Controls {
    public partial class TeamMemberControl : DevExpress.XtraEditors.XtraUserControl {
        public TeamMemberControl() {
            InitializeComponent();
        }

        private int teamMemberId;

        [Browsable(true)]
        public int TeamMemberId {
            get { return teamMemberId; }
            set {
                teamMemberId = value;

                this.chartLine.Series[0].DataFilters.ClearAndAddRange(new DevExpress.XtraCharts.DataFilter[] { 
                    new DevExpress.XtraCharts.DataFilter("TeamMemberId", "System.Int32", DevExpress.XtraCharts.DataFilterCondition.Equal, teamMemberId)});
            }
        }

        public object StatDataSource {
            set { teamMemberStatListBindingSource.DataSource = value; }
        }

        public object StatDetailDataSource {
            set { teamMemberStatDetailListBindingSource.DataSource = value; }
        }

        private void chartPie_CustomDrawSeriesPoint(object sender, DevExpress.XtraCharts.CustomDrawSeriesPointEventArgs e) {
            var chart = sender as ChartControl;

            int pointIndex = e.Series.Points.IndexOf(e.SeriesPoint);

            if (e.SeriesPoint.Argument.Equals(this.TeamMemberId.ToString())) {
                e.SeriesDrawOptions.Color = Color.FromArgb(127, 159, 48);
            } else {
                e.SeriesDrawOptions.Color = Color.FromArgb(102, 102, 102);
            }

            if (e.SeriesDrawOptions is BarDrawOptions) {
                ((BarDrawOptions)e.SeriesDrawOptions).FillStyle.FillMode = FillMode.Solid;
            }
        }

        private void teamMemberStatDetailListBindingSource_ListChanged(object sender, ListChangedEventArgs e) {
            this.lblTotal.Text = teamMemberStatDetailListBindingSource.List
                .OfType<TeamMemberStatDetail>()
                .Where(d => d.TeamMemberId == this.TeamMemberId)
                .Sum(d => d.Amount)
                .ToString("c0");
        }

        private void teamMemberStatListBindingSource_ListChanged(object sender, ListChangedEventArgs e) {
            TeamMemberStat stat = teamMemberStatListBindingSource.List
                .OfType<TeamMemberStat>()
                .FirstOrDefault(d => d.TeamMemberId == this.TeamMemberId);

            if (stat != null)
                this.lblPercent.Text = stat.Percent.ToString() + "%";
            else
                this.lblPercent.Text = "0%";
        }

        private void TeamMemberControl_Load(object sender, EventArgs e) {
            teamMemberBindingSource.DataSource = DataProvider.Instance.TeamMembers.Where(t => t.Id == this.TeamMemberId).FirstOrDefault();
        }
    }
}